package org.duoyi.striver.service.task;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.duoyi.striver.StriverApplication;
import org.duoyi.striver.consts.Area;
import org.duoyi.striver.consts.Picture;
import org.duoyi.striver.consts.TaskState;
import org.duoyi.striver.model.impl.Sleeper;
import org.duoyi.striver.service.common.CommonService;

/**
 * 竞技场任务
 */
@Slf4j
public class SportsTask extends AbstractTask {


    @Override
    public void doTask() {
        before();
        if (this.state.equals(TaskState.FINISHED)) {
            return;
        }
        while (true) {
            if (Picture.竞技场_界面标识.find()) {
                synchronized (StriverApplication.class) {
                    if (Picture.竞技场_界面标识.find()) {
                        Area.竞技场_刷新对手.leftClick();
                        int i = RandomUtil.randomInt(1, 5);
                        log.info("挑战第{}个对手", i);
                        Area.valueOf("竞技场_对手" + i).leftClick();
                        Sleeper.sleep(3000);
                        CommonService.isFighting();
                    }
                }
            } else {
                synchronized (StriverApplication.class) {
                    CommonService.openDailyPanel();
                    if (Picture.竞技场_已完成.find()) {
                        break;
                    }
                    log.info("领取竞技场任务");
                    Area.竞技场_日程领取.leftClick();
                }
            }
        }
    }

    private void before() {
        synchronized (StriverApplication.class) {
            //打开日程
            CommonService.openDailyPanel();
            //选择竞技场任务
            if (Picture.竞技场_已完成.find()) {
                this.state = TaskState.FINISHED;
                return;
            }
            log.info("领取竞技场任务");
            Area.竞技场_日程领取.leftClick();
            this.state = TaskState.RUNNING;
        }
    }
}
